public class MyBinarySearchTree {
    static class TreeNode{
        int val;
        TreeNode right;
        TreeNode left;
        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;
    //查找某个元素
    public boolean search(int key) {
        //搜索树为空
        if(root==null){
            return false;
        }
        //根节点等于key
        if(root.val==key){
            return true;
        }
        TreeNode cur=root;
        //不为空，且根节点！=key
        while (cur!=null){
            if(cur.val<key){
                cur=cur.right;
            }else if (cur.val>key){
                cur=cur.left;
            }else {
                //cur.val=key;
                return true;
            }
        }
        return false;
    }
    public  boolean insert(int val) {
        TreeNode key=new TreeNode(val);
        if (root==null){
           root=key;
           return true;
        }else {
            TreeNode node=root;
            TreeNode parent=null;
            while (node!=null){
                if(node.val>val){
                    parent=node;
                    node=node.left;
                }else if (node.val<val){
                    parent=node;
                    node=node.right;
                }else {
                    //相等
                    return false;
                }
            }
            node=key;
            if(parent.val>val){
                parent.left=node;
            }else {
                parent.right=node;
            }
        }
        return true;
    }
    public void remove(int key) {
        //找到key这个节点的位置
        if(root==null||root.val==key){
            return ;
        }
        TreeNode cur=root;
        TreeNode parent=null;
        //不为空，且根节点！=key
        while (cur!=null){
            if(cur.val<key){
                parent=cur;
                cur=cur.right;
            }else if (cur.val>key){
                parent=cur;
                cur=cur.left;
            }else {
                //cur.val=key;
                removekey(cur,parent);
            }
        }


    }
    public void removekey(TreeNode cur,TreeNode parent){
        if(cur.left==null){
            if(cur==root){
                root=cur.right;
            }else if(cur==parent.left){
                parent.left=cur.right;
            }else {
                parent.right=cur.right;
            }
        }else if(cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        }else {
            //cur.left！=null&&cur.right!=null
            TreeNode node=cur;
            parent=cur;
            cur=cur.right;
            while (cur.left!=null){
                parent=cur;
                cur=cur.left;
            }
            node.val=cur.val;
            //删除替代元素
            if(parent.left==cur){
                parent.left=cur.right;
            }else {
                parent.right=cur.right;
            }
        }
    }





}
